import store from '@/store'
import { getFormatDate } from '@/afterSales/utils/common'
const defaultString: string[] = ['text', 'multiText', 'number', 'email', 'phone', 'richText']
const defaultArr: string[] = ['sign', 'associateSpare', 'associateService', 'cascadeSelect', 'region']
export function emptyField (item) {
  if (defaultArr.includes(item.fieldType) || item.fieldType === 'checkbox' || item.fieldType === 'accessory') {
    return []
  } else if (defaultString.includes(item.fieldType)) {
    return ''
  } else if (item.fieldType === 'radio' || item.fieldType === 'associateUser') {
    return null
  }
  return ''
}
export function numberResultFn (number1: any, { decimalPoint }) {
  if (typeof number1 === 'number') {
    return number1.toFixed(decimalPoint)
  }
  return number1
}
export function initField (item: any) {
  if (item.fieldType === 'number') {
    const v = item.defaultType === 2 ? undefined : (item.defaultValue || '')
    return v ? numberResultFn(Number(v), item) : v
  } else if (item.fieldType === 'dateTime') {
    if (item.defaultType === 2) {
      return getFormatDate(new Date());
    } else {
      return ''
    }
  } else if (defaultString.includes(item.fieldType)) {
    return item.defaultType === 2 ? undefined : (item.defaultValue || '')
  } else if (defaultArr.includes(item.fieldType)) {
    return item.defaultValue ? [...item.defaultValue] : []
  } else if (item.fieldType === 'department') {
    let { fixedValue = '', dynamicValue = '' } = item.defaultValueObject || {}
    fixedValue = fixedValue ? fixedValue.split(',') : []
    dynamicValue = []
    if (item.selectType === 1) {
      return fixedValue[0] || dynamicValue[0]
    } else {
      return [...fixedValue, ...dynamicValue].join(',')
    }
    return fixedValue
  } else if (item.fieldType === 'member') {
    let { fixedValue = '', dynamicValue = '' } = item.defaultValueObject || {}
    fixedValue = fixedValue ? fixedValue.split(',') : []
    // 当前用户
    if (dynamicValue && dynamicValue.includes('currentUser')) {
      dynamicValue = [store.state.common.session.user._id]
    } else {
      dynamicValue = []
    }
    if (item.selectType === 1) {
      return fixedValue[0] || dynamicValue[0]
    } else {
      return [...fixedValue, ...dynamicValue].join(',')
    }
  } else if (item.fieldType === 'radio') {
    return item.defaultValueRadio || null
  } else if (item.fieldType === 'checkbox') {
    return item.defaultValueCheckbox || null
  } else if (item.fieldType === 'accessory') {
    return item.defaultAttachList || null
  }
}
